package com.example.rain.myapplication;

import android.content.ContentResolver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.net.URI;

public class MainActivity extends AppCompatActivity {
    private MyOpenHelp myOpenHelp;
    private SQLiteDatabase db;
    private  int i = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File sdcardPath = Environment.getExternalStorageDirectory();
        myOpenHelp = new MyOpenHelp(this,sdcardPath.getAbsolutePath() + "/leilei.db",null,1);
        db =myOpenHelp.getWritableDatabase();

        Log.i("lili", sdcardPath + "...");
    }

    public void storeDB(View view) {
        //String ddl = "insert into users(uanme),values('leilei'+"+i+++")";
        String ddl = "insert into user1 (uname) values(?)";
        try {

            db.beginTransaction();
            db.execSQL(ddl, new String[]{"leilei" + i++});
            db.setTransactionSuccessful();
        } catch (Exception e){

        }
        finally {
            db.endTransaction();
        }
    }

    public void getFromDB(View view) {
        String sql = "select _id,uname from user1 where _id>?";
        Cursor c = db.rawQuery(sql, new String[]{"2"});
        while(c.moveToNext()){
            String uname = c.getString(1);
            int _id = c.getInt(c.getColumnIndex("_id"));
            Log.i("lili",uname+"..."+_id);
        }

    }

    public void query(View view) {
        Uri uri = Uri.parse("content://leilei.cn");
      ContentResolver cr =  getContentResolver();
        cr.query(uri,null,null,null,null);
    }
}
